﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace NodeBasedSkillEditor.View
{
    public class RectSelector : Shape
    {
        public Rect RangeRect {
            get { return (Rect)GetValue(RangeRectProperty); }
            set { SetValue(RangeRectProperty, value); }
        }
        public static readonly DependencyProperty RangeRectProperty =
            DependencyProperty.Register("RangeRect", typeof(Rect), typeof(RectSelector),
                new FrameworkPropertyMetadata(new Rect(), FrameworkPropertyMetadataOptions.AffectsRender));

        public bool IsIntersect {
            get { return (bool)GetValue(IsIntersectProperty); }
            set { SetValue(IsIntersectProperty, value); }
        }
        public static readonly DependencyProperty IsIntersectProperty =
            DependencyProperty.Register("IsIntersect", typeof(bool), typeof(RectSelector),
                new FrameworkPropertyMetadata(false));

        public double CornerRadius {
            get { return (double)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }
        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(double), typeof(RectSelector),
                new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));


        protected override Geometry DefiningGeometry => Geometry.Empty;

        Pen _strokePen = null;

        static RectSelector()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(RectSelector), new FrameworkPropertyMetadata(typeof(RectSelector)));
        }

        public RectSelector()
        {
            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            _strokePen = new Pen(Stroke, StrokeThickness);
            _strokePen.Freeze();
        }

        public void Reset()
        {
            RangeRect = new Rect(new Point(), Size.Empty);
            IsIntersect = false;
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            drawingContext.DrawRoundedRectangle(Fill, _strokePen, RangeRect, CornerRadius, CornerRadius);
        }

    }
}
